package com.situ.mall.order.controller;


import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.situ.mall.common.util.Result;
import com.situ.mall.order.pojo.query.OrderInfoQuery;
import com.situ.mall.order.pojo.vo.OrderInfoVO;
import com.situ.mall.order.service.IOrderInfoService;
import com.situ.mall.pojo.entity.order.OrderInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 订单 前端控制器
 * </p>
 *
 * @author CKX
 * @since 2024-10-16
 */
@RestController
@RequestMapping("/order/orderInfo")
public class OrderInfoController {

    @Autowired
    private IOrderInfoService orderInfoService;

    @PostMapping("/add")
    public Result add(@RequestBody OrderInfo orderInfo) {
        //orderInfo里面有userId和addressId
        orderInfoService.add(orderInfo);
        return Result.ok("添加成功");
    }

    @GetMapping("/list")
    public Result<List<OrderInfoVO>> list(OrderInfoQuery orderInfoQuery) {
        List<OrderInfoVO> list = orderInfoService.list(orderInfoQuery);
        return Result.ok(list);
    }

    @GetMapping("/selectByPage")
    public Result<Map<String, Object>> selectByPage(OrderInfoQuery orderInfoQuery) {
        Map<String, Object> map = orderInfoService.selectByPage(orderInfoQuery);
        return Result.ok(map);
    }

    @PostMapping("/settled")
    public Result settled(@RequestBody OrderInfo orderInfo) {
        UpdateWrapper<OrderInfo> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", orderInfo.getId()).set("order_status", 1);
        orderInfoService.update(updateWrapper);
        return Result.ok("付款成功");
    }

    @PostMapping("/cancel")
    public Result cancel(@RequestBody OrderInfo orderInfo) {
        UpdateWrapper<OrderInfo> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", orderInfo.getId()).set("order_status", 4).set("cancel_reason", "用户取消");
        orderInfoService.update(updateWrapper);
        return Result.ok("取消成功");
    }

    @PutMapping("/adminCancel/{id}")
    public Result adminCancel(@PathVariable("id") Integer id) {
        UpdateWrapper<OrderInfo> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", id).set("order_status", 4).set("cancel_reason", "商家取消");
        orderInfoService.update(updateWrapper);
        return Result.ok("取消成功");
    }

    @PostMapping("/received")
    public Result received(@RequestBody OrderInfo orderInfo) {
        UpdateWrapper<OrderInfo> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", orderInfo.getId()).set("order_status", 3);
        orderInfoService.update(updateWrapper);
        return Result.ok("收货成功");
    }

    @PutMapping("/shipment/{id}")
    public Result shipment(@PathVariable Integer id) {
        UpdateWrapper<OrderInfo> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", id).set("order_status", 2);
        orderInfoService.update(updateWrapper);
        return Result.ok("发货成功");
    }
}

